//
//  WmcBubbleArrowView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/12.
//

import UIKit

class WmcBubbleArrowView: WmcBaseView {
    var direction: WmcBubbleVM.Direction = .top

    lazy var shapeLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        return layer
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
        let width = frame.width
        let height = frame.height
        let centerX = width / 2
        let centerY = height / 2

        let topLeftPoint = CGPoint(x: 0, y: 0)
        let topCenterPoint = CGPoint(x: centerX, y: 0)
        let topRightPoint = CGPoint(x: width, y: 0)

        let centerLeftPoint = CGPoint(x: 0, y: centerY)
        let centerRightPoint = CGPoint(x: width, y: centerY)

        let bottomLeftPoint = CGPoint(x: 0, y: height)
        let bottomCenterPoint = CGPoint(x: centerX, y: height)
        let bottomRightPoint = CGPoint(x: width, y: height)

        let bezierPath = UIBezierPath()
        switch direction {
        case .top:
            bezierPath.move(to: topLeftPoint)
            bezierPath.addLine(to: topRightPoint)
            bezierPath.addLine(to: bottomCenterPoint)
        case .left:
            bezierPath.move(to: topLeftPoint)
            bezierPath.addLine(to: centerRightPoint)
            bezierPath.addLine(to: bottomLeftPoint)
        case .bottom:
            bezierPath.move(to: bottomLeftPoint)
            bezierPath.addLine(to: topCenterPoint)
            bezierPath.addLine(to: bottomRightPoint)
        case .right:
            bezierPath.move(to: topRightPoint)
            bezierPath.addLine(to: centerLeftPoint)
            bezierPath.addLine(to: bottomRightPoint)
        }
        bezierPath.close()
        shapeLayer.path = bezierPath.cgPath
        layer.mask = shapeLayer
    }

    override func makeUI() {}
}
